package com.sunyur.tailor.channel.service.inner.workflow.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.workflow.param.WorkFlowSyncTaskParam;
import com.sunyur.tailor.channel.service.inner.workflow.service.SuryurWorkFlowService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @description: 工作流接口实现类
 * @author: wjh
 * @time: 2021/5/10 0010 15:26
 */
@Service("suryurWorkFlowService")
public class SuryurWorkFlowServiceImpl implements SuryurWorkFlowService {

    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SuryurWorkFlowServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SuryurWorkFlowServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 工作流同步审批进度给主线
     */
    @Value("${sunyur.api.workflowTaskComplete}")
    private String workflowTaskComplete;

    @Override
    public ChannelResult<Boolean> workflowTaskComplete(WorkFlowSyncTaskParam workFlowSyncTaskParam) throws Exception {
        LOGGER_INFO.info("SuryurWorkFlowServiceImpl workflowTaskComplete param is {}", JSON.toJSONString(workFlowSyncTaskParam));
        if (ObjectUtils.isEmpty(workFlowSyncTaskParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SuryurWorkFlowServiceImpl workflowTaskComplete param is null");
        }
        // 调用主线接口
        SunyurResult<Boolean> sunyurResult = sunyurClient.execPost(workflowTaskComplete, JSON.toJSONString(workFlowSyncTaskParam), new TypeReference<SunyurResult<Boolean>>() {});
        // 主线报错
        if (!"success".equals(sunyurResult.getCode())) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }


}
